package com.example.presentshopping.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import com.example.presentshopping.R;

import org.apache.commons.lang3.StringUtils;

// 升级弹框。
public class UpDateDialog extends Dialog {

    private Context context;
    private String info;
    private String size;
    private OnBtnClickListener onBtnClickListener;

    private TextView upDateInfo;
    private TextView upDateSize;
    private TextView upDateCancel;
    private TextView upDateConfirm;

    private static final String TAG = "UpDateDialog";

    private String isUpdate;// 空或1：不升级； 2：普通升级； 3：强制升级。

    public UpDateDialog(Context context, String isUpdate) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.isUpdate = isUpdate;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_updata);

        setCancelable(false);
        upDateInfo = (TextView) findViewById(R.id.upDateInfo);
        upDateSize = (TextView) findViewById(R.id.upDateSize);
        upDateConfirm = (TextView) findViewById(R.id.upDateConfirm);
        upDateCancel = (TextView) findViewById(R.id.upDateCancel);

        upDateCancel.setVisibility(View.VISIBLE);
        // 3强制更新
        if (StringUtils.equals(isUpdate, "1")) {
            upDateCancel.setVisibility(View.GONE);
        }

        upDateInfo.setText(info);
        upDateSize.setText(context.getResources().getString(R.string.NewVersionSize) + size);

        upDateConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upDateConfirm.setClickable(false);
                upDateConfirm.setText(context.getResources().getString(R.string.Downloading));
                onBtnClickListener.onConfirmClick(UpDateDialog.this);
                // 1强制更新
                if (!StringUtils.equals(isUpdate, "1")) {
                    dismiss();
                }
            }
        });
        upDateCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnClickListener.onCancelClick(UpDateDialog.this);
                dismiss();
            }
        });
    }

    // 升级完后初始化UI
    public void recover() {
        upDateConfirm.setClickable(true);
        // upDateConfirm.setBackgroundResource(R.drawable.button_ellipse_color_main_bg_20);
        upDateConfirm.setText(context.getResources().getString(R.string.UpdateNow));
    }

    @Override
    protected void onStop() {
        super.onStop();
        recover();
    }

    public void setContent(String info, String size, OnBtnClickListener onBtnClickListener) {
        this.info = info;
        this.size = size;
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onConfirmClick(UpDateDialog upDateDialog);

        void onCancelClick(UpDateDialog upDateDialog);
    }

}
